package com.axend.aerosense.common.connect.bean;

import com.axend.aerosense.base.utils.ByteUtils;

import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;

/**
 * IP与端口用英文的“,”隔开
 * wifi账号与密码用英文的“,”隔开
 */
public class BleMessageEntity {

    private Tag tag;  //消息标志
    private String message;  //消息体
    private State state;  //消息发送状态

    private byte[] deviceId;  //设备ID
    private byte[] deviceVersion;  //设备版本号

    public BleMessageEntity(State state) {
        this.state = state;
    }

    public BleMessageEntity(Tag tag) {
        this.tag = tag;
    }

    public BleMessageEntity() {
    }

    public BleMessageEntity(Tag tag, String message) {
        this.tag = tag;
        this.message = message;
    }

    public String getDeviceId() {
        return ByteUtils.bytesToHexString(deviceId);
    }

    public void setDeviceId(byte[] deviceId) {
        this.deviceId = deviceId;
    }

    public String getDeviceVersion() {
        return getHardwareVersionByString(deviceVersion);
    }

    public void setDeviceVersion(byte[] deviceVersion) {
        this.deviceVersion = deviceVersion;
    }

    public Tag getTag() {
        return tag;
    }

    public void setTag(Tag tag) {
        this.tag = tag;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public void setState(State state) {
        this.state = state;
    }

    public byte[] getMessage() {
        if (tag == Tag.serverInfo){
            return ("[" + message + "]").getBytes(StandardCharsets.UTF_8);
        }else if (tag == Tag.networkInfo) {
            return ("(" + message + ")").getBytes(StandardCharsets.UTF_8);
        }else if (tag == Tag.getDeviceUUID){
            return ByteUtils.hexStringToBytes("130101019B000000102700000006041000000000");
        }else if (tag == Tag.resetDevice){
            return ByteUtils.hexStringToBytes("130101019B000000102700000006041100000000");
        }
        return null;
    }

    public State getState() {
        return state;
    }

    public enum State{
        setSuccess(0x01),
        setFail(0x00),
        ;

        private byte b;

        State(int b) {
            this.b = (byte) b;
        }

        public byte getByte() {
            return b;
        }
    }

    public enum Tag{
        serverInfo,
        networkInfo,
        getDeviceUUID,
        resetDevice,
        ;
    }

    @Override
    public String toString() {
        return "BleMessageEntity{" +
                "tag=" + tag +
                ", message='" + message + '\'' +
                ", state=" + state +
                ", deviceId=" + getDeviceId() +
                ", deviceVersion=" + getDeviceVersion() +
                '}';
    }

    private String getHardwareVersionByString(byte[] bytes){
        if (null == bytes || bytes.length != 4) return null;
        ByteBuffer buffer = ByteBuffer.allocate(bytes.length);
        buffer.put(bytes);
        buffer.rewind();
        String str = "";
        //将每个字节转换成Int
        for (int i = 0; i < 3; i++){
            byte b = buffer.get();
            byte[] temp = new byte[4];
            temp[3] = b;
            //如果是个位数前补零
            String hexString = String.format("%02d", ByteUtils.byteToInt(temp));
            if (i == bytes.length -2){
                str += hexString;
            }else {
                str += hexString + ".";
            }
        }
        return str;
    }
}
